package jp.ac.aiit.spg.web.wbs;

import java.util.*;

import javax.ejb.EJB;

import jp.ac.aiit.spg.common.*;
import jp.ac.aiit.spg.entity.*;
import jp.ac.aiit.spg.service.SpgService;

import org.seasar.teeda.extension.annotation.convert.DateTimeConverter;
import org.seasar.teeda.extension.annotation.validator.Required;

public class CreateTaskPage {

	@EJB
	private SpgService ss;

	@Required
	private String taskName;

	@Required
	private int workingTime;

	@DateTimeConverter(pattern = "yyyy/MM/dd HH")
	private Date startTimePlanned;

	private List<SpgMember> members;

	@Required
	private int spgmemberid;

	private List<SpgMemberItem> spgmemberidItems;

	@Required
	private int assignStatusid;

	private List<Map> assignStatusidItems;

	private int projectid;
	
	private boolean StartTimeSpecified;
	
	private List<SpgTask> parentTasks;
	
	@Required
	private int parentTaskid;
	
	private List<SpgTaskItem2> parentTaskidItems;
	

	private List<SpgTask> predecessorTasks;
	
	@Required
	private Integer[] predecessorTaskids;
	
	private  List<SpgTaskItem2> predecessorTaskidsItems;
	
	private String deliverableMemo;
	
	public String getDeliverableMemo() {
		return deliverableMemo;
	}

	public void setDeliverableMemo(String deliverableMemo) {
		this.deliverableMemo = deliverableMemo;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public List<SpgMember> getMembers() {
		return members;
	}

	public void setMembers(List<SpgMember> members) {
		this.members = members;
	}

	public List<SpgMemberItem> getSpgmemberidItems() {
		return spgmemberidItems;
	}

	public void setSpgmemberidItems(List<SpgMemberItem> spgmembermanyItems) {
		this.spgmemberidItems = spgmembermanyItems;
	}

	public int getSpgmemberid() {
		return spgmemberid;
	}

	public void setSpgmemberid(int spgmembermany) {
		this.spgmemberid = spgmembermany;
	}

	public Date getStartTimePlanned() {
		return startTimePlanned;
	}

	public void setStartTimePlanned(Date startTimePlanned) {
		this.startTimePlanned = startTimePlanned;
	}

	public int getWorkingTime() {
		return workingTime;
	}

	public void setWorkingTime(int workingTime) {
		this.workingTime = workingTime;
	}

	public List<Map> getAssignStatusidItems() {
		return assignStatusidItems;
	}

	public void setAssignStatusidItems(List<Map> assignStatusidItems) {
		this.assignStatusidItems = assignStatusidItems;
	}

	public int getAssignStatusid() {
		return assignStatusid;
	}

	public void setAssignStatusid(int assignStatusid) {
		this.assignStatusid = assignStatusid;
	}

	public int getProjectid() {
		return projectid;
	}

	public void setProjectid(int projectid) {
		this.projectid = projectid;
	}

	public boolean isStartTimeSpecified() {
		return StartTimeSpecified;
	}

	public void setStartTimeSpecified(boolean StartTimeSpecified) {
		this.StartTimeSpecified = StartTimeSpecified;
	}

	
	public int getParentTaskid() {
		return parentTaskid;
	}

	public void setParentTaskid(int parentTaskid) {
		this.parentTaskid = parentTaskid;
	}

	public List<SpgTaskItem2> getParentTaskidItems() {
		return parentTaskidItems;
	}

	public void setParentTaskidItems(List<SpgTaskItem2> parentTaskidItems) {
		this.parentTaskidItems = parentTaskidItems;
	}

	public List<SpgTask> getParentTasks() {
		return parentTasks;
	}

	public void setParentTasks(List<SpgTask> parentTasks) {
		this.parentTasks = parentTasks;
	}

	public Integer[] getPredecessorTaskids() {
		return predecessorTaskids;
	}

	public void setPredecessorTaskids(Integer[] predecessorTaskids) {
		this.predecessorTaskids = predecessorTaskids;
	}

	public List<SpgTaskItem2> getPredecessorTaskidsItems() {
		return predecessorTaskidsItems;
	}

	public void setPredecessorTaskidsItems(
			List<SpgTaskItem2> predecessorTaskidsItems) {
		this.predecessorTaskidsItems = predecessorTaskidsItems;
	}

	public List<SpgTask> getPredecessorTasks() {
		return predecessorTasks;
	}

	public void setPredecessorTasks(List<SpgTask> predecessorTasks) {
		this.predecessorTasks = predecessorTasks;
	}

	public Class initialize() {

		taskName = "";

		return null;
	}

	public Class prerender() {

		// チームメンバーの名前をとってきて表示コンポーネントに詰める。但し教員の場合は除く。
		prepareMemberOptions();

		// 承認状態の選択肢を表示コンポーネントに詰める
		prepareAssignOptions();

		// 親タスクとして指定できるタスクのリストをとってきて表示コンポーネントに詰める。
		//（ルートも追加する。）
		prepareParentOptions();

		// 先行タスクとして指定できるタスクのリストをとってきて表示コンポーネントに詰める。
		//（ルートはなし。先行タスク無を明示的に指定もできるようにする）
		preparePredecessorOptions();
		
		return null;
	}

	private void preparePredecessorOptions() {
		String[] sortOrder = {"wbsNumber"};
		predecessorTasks = ss.listAllProjectTask(projectid, sortOrder);

		predecessorTaskidsItems = new ArrayList<SpgTaskItem2>();

		SpgTaskItem2 unselectitem = new SpgTaskItem2();
		unselectitem.setValue(SpgConstant.NOT_A_TASK);
		unselectitem.setLabel("先行タスクなし");
		predecessorTaskidsItems.add(unselectitem);
		
		for (SpgTask t : predecessorTasks ) {
			SpgTaskItem2 item2 = new SpgTaskItem2();
			item2.setValue(t.getId());
			item2.setLabel(t.getWbsNumber() + " : " + t.getName());
			predecessorTaskidsItems.add(item2);
		}

		// 表示上の初期値を”先行タスクなし”にする。
		predecessorTaskids = new Integer[1];
		predecessorTaskids[0] = SpgConstant.NOT_A_TASK;
	}

	private void prepareParentOptions() {
		String[] sortOrder = {"wbsNumber"};
		parentTasks = ss.listAllProjectTask(projectid, sortOrder);
		SpgTask rootTask = ss.getRootTask(projectid);
		parentTasks.add(0, rootTask);
		
		parentTaskidItems = new ArrayList<SpgTaskItem2>();
		for (SpgTask t : parentTasks ) {
			SpgTaskItem2 item = new SpgTaskItem2();
			item.setValue(t.getId());
			item.setLabel(t.getWbsNumber() + " : " + t.getName());
			parentTaskidItems.add(item);
		}
	}

	private void prepareAssignOptions() {
		assignStatusidItems = new ArrayList<Map>();
		Map map1 = new HashMap();
		map1.put("label", SpgAssignStatus.CREATED.toString());
		map1.put("value", new Integer(1));
		assignStatusidItems.add(map1);
		Map map2 = new HashMap();
		map2.put("label", SpgAssignStatus.ASSIGNED.toString());
		map2.put("value", new Integer(2));
		assignStatusidItems.add(map2);
	}

	private void prepareMemberOptions() {
		spgmemberidItems = new ArrayList<SpgMemberItem>();
		// ここでprojectIdが全画面から引き継がれてこない。@SubapplicationScopeなのになぜ？とりあえずhiddenで回避
		members = ss.listAllMember(projectid); // 12/12
		for (SpgMember m : members) {
			if (m.getRole() != SpgRole.TEACHER) {
				SpgMemberItem item = new SpgMemberItem();
				item.setValue(m.getId());
				item.setLabel(m.getUser().getName() + " : " + m.getUser().getDescription());
				spgmemberidItems.add(item);
			}
		}
	}

	public Class doCreate() {
		
		
		SpgTaskTemplete stt = new SpgTaskTemplete();
		stt.name = taskName;
		stt.workingTime = workingTime;
		stt.isStartTimeSpecifyedByUser = StartTimeSpecified;
		stt.startTimePlanned = startTimePlanned;
		switch (assignStatusid) {
		case 1:
			stt.assignment = SpgAssignStatus.CREATED;
			break;
		case 2:
			stt.assignment = SpgAssignStatus.ASSIGNED;
			break;
		default:
			;
		}		
		stt.deliverableMemo = deliverableMemo ;  
		stt.assignMemo = "";
		stt.projectId = projectid;
		stt.memberId = spgmemberid;
		stt.parentTaskId = parentTaskid;
		
		stt.PredecessorIds = new ArrayList<Integer>();
		if ( predecessorTaskids.length != 0 ) {
			for (Integer i : predecessorTaskids) {
				stt.PredecessorIds.add(i);
			}
		}
		
		ss.createTask(stt);
		
		return ConfirmTaskPage.class;
	}

}
